<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form :inline="true" :model="listQuery">
        <el-form-item label="状态">
          <el-select v-model="listQuery.orderStatus" filterable placeholder="请选择订单状态" @change="handleSearch">
            <el-option label="全部" value="" />
            <el-option label="待付款" value="0" />
            <el-option label="已取消" value="1" />
            <el-option label="已付款" value="2" />
            <el-option label="配送中" value="3" />
            <el-option label="已完成" value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="关键字">
          <el-input v-model="listQuery.keyword" placeholder="请输入关键字" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain @click="handleSearch">查询</el-button>
          <el-button plain @click="handleReset">重置</el-button>

        </el-form-item>
      </el-form>
    </div>
    <div class="table-container">
      <el-table
        ref="multipleTable"
        :data="list"
        height="100%"
        style="width: 100%"
        @sort-change="sortChange"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" width="60" label="序号" />
        <el-table-column prop="gmtCreated" label="下单时间" width="140" sortable>
          <template slot-scope="scope">
            <span>{{ T.formatDate(scope.row.gmtCreated,'yyyy-MM-dd hh:mm:ss') }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="订单号" width="140" />
        <el-table-column prop="orderName" label="订单名称" min-width="100" />
        <el-table-column prop="grandTotal" label="订单价格" width="120">
          <template slot-scope="scope">
            <span style="font-size: 14px;">
              <span class="color-primary">{{ T.currency(scope.row.grandTotal,'￥') }}</span>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="orderStatus" label="状态" width="100" sortable>
          <template slot-scope="scope">
            <span v-if="scope.row.orderStatus===0" class="color-danger">待付款</span>
            <span v-if="scope.row.orderStatus===1" class="color-default">已取消</span>
            <span v-if="scope.row.orderStatus===2" class="color-success"> 已付款</span>
            <span v-if="scope.row.orderStatus===3" class="color-warning">配送中</span>
            <span v-if="scope.row.orderStatus===4" class="color-primary">已完成</span>
          </template>
        </el-table-column>
        <el-table-column prop="userRealName" label="姓名" width="100" />
        <el-table-column prop="userPhone" label="电话" width="120" />
        <el-table-column prop="address" label="地址" min-width="160" />
      </el-table>
      <div class="pagination_handler">
        <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchData" />
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      multipleSelectionRecords: [],
      list: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: 20,
        keyword: '',
        orderStatus: '',
        sort: '-gmtCreated'
      }
    }
  },
  created() {
    this.fetchData()
    this.fetchProductCategory()
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    fetchProductCategory() {
      this.$store.dispatch('productCategoryList').then((res) => {
        this.categoryOptions = res
      }).catch((_) => {})
    },
    /* 全选/反选*/
    handleSelectionChange(val) {
      this.multipleSelectionRecords = val
    },
    /* 表格列排序*/
    sortChange(data) {
      const { prop, order } = data
      if (prop) {
        if (order === 'ascending') {
          this.listQuery.sort = '+' + prop
        } else {
          this.listQuery.sort = '-' + prop
        }
      } else {
        this.listQuery.sort = '-gmtCreated'
      }
      this.handleSearch()
    },
    fetchData() {
      this.$store.dispatch('orderHeaderPage', this.listQuery).then((res) => {
        this.list = res.records
        this.total = res.total
      }).catch((_) => {})
    },
    handleSearch() {
      this.listQuery = {
        ...this.listQuery,
        page: 1
      }
      this.fetchData()
    },
    handleReset() {
      this.listQuery = {
        ...this.listQuery,
        page: 1,
        limit: 20,
        keyword: undefined,
        sort: '-gmtCreated',
        orderStatus: ''
      }
      this.fetchData()
    }
  }
}
</script>
  <style lang="scss" scoped>

  </style>

